﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using Gencode.PrsCommon.App;
using Gencode.Common.Errorhandling;

namespace Gencode.PrsCommon.Data.Plan
{
    public class SaldoTable
    {
        public DataTable Table
        {
            get;
            set;
        }

        public void AddItem(int prsID, int planID, int abteilungID)
        {
            if (Table == null)
                InitTable();

            DataView dv = PlanData.GetSaldo(prsID, abteilungID, planID);

            double soll = Util.RoundDouble(double.Parse(dv[0].Row["soll"].ToString()));
            double haben = Util.RoundDouble(double.Parse(dv[0].Row["haben"].ToString()));
            double vortrag = Util.RoundDouble(double.Parse(dv[0].Row["vortrag"].ToString()));
			double anpassung = Util.RoundDouble(double.Parse(dv[0].Row["anpassung"].ToString()));

            DataRow row = Table.NewRow();
            row["Vortr"] = vortrag;
            row["Soll"] = soll;
            row["Haben"] = haben;
			row["Anp"] = anpassung;
            row["Diff"] = Util.RoundDouble(vortrag + (haben - soll) + anpassung);
            Table.Rows.Add(row);
        }

        public void AddItem()
        {
            if (Table == null)
                InitTable();

            DataRow row = Table.NewRow();
            row["Vortr"] = "";
            row["Soll"] = "";
            row["Haben"] = "";
			row["Anp"] = "";
            row["Diff"] = "";
            Table.Rows.Add(row);
        }

        public void InitTable()
        {
            Table = new DataTable();
            Table.Columns.Add("Vortr", typeof(string));
            Table.Columns.Add("Soll", typeof(string));
            Table.Columns.Add("Haben", typeof(string));
			Table.Columns.Add("Anp", typeof(string));
            Table.Columns.Add("Diff", typeof(string));
        }

        public void UpdateRow(int index, int prsID, int planID, int abteilungID)
        {
            if (Table != null)
            {

                if (prsID > 0)
                {
                    DataView dv = PlanData.GetSaldo(prsID, abteilungID, planID);

                    double soll = Util.RoundDouble(double.Parse(dv[0].Row["soll"].ToString()));
                    double haben = Util.RoundDouble(double.Parse(dv[0].Row["haben"].ToString()));
                    double vortrag = Util.RoundDouble(double.Parse(dv[0].Row["vortrag"].ToString()));
                    double anpassung = Util.RoundDouble(double.Parse(dv[0].Row["anpassung"].ToString()));

                    Table.Rows[index]["Vortr"] = vortrag;
                    Table.Rows[index]["Soll"] = soll;
                    Table.Rows[index]["Haben"] = haben;
                    Table.Rows[index]["Anp"] = anpassung;
                    Table.Rows[index]["Diff"] = Util.RoundDouble(vortrag + (haben - soll) + anpassung);
                }
                else
                    Gencode.Common.ActionLogger.Logger.LoggMessage(Gencode.Common.ActionLogger.MessageType.DATA_ERROR,
                        "Prsid " + prsID + " wollte ein update im saldogrid von plan " + planID + "  bei index " + index + " machen!");
            }
        }
    }
}
